package com.smile.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smile.data.dto.StaffQueryDto
import com.smile.data.entity.StaffEntity
import com.smile.mapper.StaffMapper
import com.smile.response.ResultEmpty
import com.smile.response.ResultPage
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
class StaffService {
    @Autowired
    lateinit var mapper: StaffMapper;

    fun query(dto: StaffQueryDto): ResultPage<StaffEntity> {
        var page = Page<StaffEntity>(dto.page, dto.size);
        var query = LambdaQueryWrapper<StaffEntity>();
        query.eq(
            !dto.status.isNullOrEmpty(), StaffEntity::status, dto.status
        ).and(!dto.keywords.isNullOrEmpty()) {
            it.like(StaffEntity::username, dto.keywords)
        }
        return mapper.selectPage(page, null).run { ResultPage(page) };
    }

    fun create(dto: StaffEntity): ResultEmpty {
        return mapper.insert(dto).run { ResultEmpty() };
    }

    fun update(id: String, dto: StaffEntity): ResultEmpty {
        dto.id = id;
        return mapper.updateById(dto).run { ResultEmpty() };
    }

    fun remove(id: String): ResultEmpty {
        return mapper.deleteById(id).run { ResultEmpty() };
    }
}
